﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Heritage.Codebox;
using System.Threading;
using Heritage.Logging;

namespace Heritage.Controllers
{
    public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Utils.IsAccessAllowed() || (filterContext.Controller is ErrorController))
            {
                Utils.UpdateLastVisit();
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectResult("/Error/AccessDenied");
                return;
            }
        }

        protected override void OnException(ExceptionContext filterContext)
        {

            LogService.Error(Utils.GetPrincipalName(), Constants.COMPONENT_BASE, filterContext.Exception.ToString());
            base.OnException(filterContext);
        }
    }

    internal class AuthorizedValidatedAttribute : ActionFilterAttribute {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Utils.IsAuthenticated())
            {
                if (Utils.IsUserValidated())
                {
                    base.OnActionExecuting(filterContext);
                }
                else
                {
                    filterContext.Result = new RedirectResult("/Auth/NeedValidation");
                    return;
                }
            }
            else
            {
                filterContext.Result = new RedirectResult("/");
                return;
            }
        }
    }
    internal class GameMasterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Utils.IsGameMasterAndAbove()) 
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectResult("/");
                return;
            }
        }
    }
    internal class ModeratorOrOwnerAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Utils.IsModeratorAndAbove())
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                if (Utils.ImOwner(filterContext.ActionParameters["id"] as string))
                {
                    base.OnActionExecuting(filterContext);
                }
                else
                {
                    filterContext.Result = new RedirectResult("/");
                    return;
                }
            }
        }
    }
    internal class ModeratorAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Utils.IsModeratorAndAbove())
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectResult("/");
                return;
            }
        }
    }
    internal class AdminAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Utils.IsAdmin())
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectResult("/");
                return;
            }
        }
    }
    internal class AuthorizedAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Utils.IsAuthenticated())
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectResult("/");
                return;
            }
        }
    }
    internal class NoCache : ActionFilterAttribute, IActionFilter
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();

            base.OnResultExecuting(filterContext);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
        }
    }

}
